package com.link.base.base.product.controller;

import com.link.base.base.product.dao.mybatis.mapper.PartsCostLineMapper;
import com.link.base.base.product.model.PartsCost;
import com.link.base.base.product.service.PartsCostService;
import com.link.base.base.project.dao.mybatis.mapper.QuoteRoundMapper;
import com.link.base.base.project.model.QuoteRound;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 成本信息汇总临时对象
 *
 * @date 2023-08-15 09:04:14
 */
@Controller
@RequestMapping("/link/partsCost")
public class PartsCostController extends BasicController<PartsCost> {

    private static final Logger logger = LogManager.getLogger(PartsCostController.class);

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private PartsCostService partsCostService;

    @Resource
    private PartsCostLineMapper partsCostLineMapper;

    @Resource
    private QuoteRoundMapper quoteRoundMapper;

    @Override
    public BasicService<PartsCost> getBasicService() throws Exception {
        return partsCostService;
    }


    public void beforDelete(PartsCost entity, HttpServletRequest request) throws Exception {
        PartsCost newEntity = partsCostService.queryById(entity.getId());
        if (null == newEntity.getProjectId()) {
            throw new RuntimeException("项目Id为空，请联系管理员!");
        }
        QuoteRound param = new QuoteRound();
        param.setProjectHeadId(newEntity.getProjectId());
        List<QuoteRound> list = quoteRoundMapper.queryByProjectId(param);
        if (!CollectionUtils.isEmpty(list)) {
            throw new RuntimeException("已生成对外报价数据，不能删除！");
        }
    }

    public void afterDelete(PartsCost entity, HttpServletRequest request) throws Exception {
        partsCostLineMapper.deleteByHeadId(entity.getId());
    }




    @RequestMapping({"/queryExcluePojectIdPickListPage"})
    @ResponseBody
    public Map<String, Object> queryExcluePojectIdPickListPage(@JsonParam QueryParams qps,
                                                  @JsonParam PartsCost entity,
                                                  HttpSession session,
                                                  HttpServletRequest request,
                                                  HttpServletResponse response) {
        HashMap result = new HashMap(8);

        try {
            if (null == entity.getProjectId()) {
                throw new RuntimeException("项目Id不能为空!");
            }
            List<PartsCost> list = partsCostService.queryExcluePojectIdPickListPage(entity);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(entity, list.size()));
            result.put("code", "200");
        } catch (Exception var10) {
            logger.error("BasicController捕获异常: ", var10);
            result.put("success", false);
            result.put("rows", new ArrayList());
            result.put("result", var10.getMessage());
            result.put("code", "400");
        }

        return result;
    }

}
